package com.ruoyi.tb.lmt.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.tb.lmt.domain.bo.TbLmtrepairWorkdetailBo;
import com.ruoyi.tb.lmt.domain.vo.AcceptCountVo;
import com.ruoyi.tb.lmt.domain.vo.MessageCountVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbMessageNoticeVo;
import com.ruoyi.tb.lmt.domain.bo.TbMessageNoticeBo;
import com.ruoyi.tb.lmt.service.ITbMessageNoticeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息通知
 *
 * @author ruoyi
 * @date 2024-01-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/messageNotice")
public class TbMessageNoticeController extends BaseController {

    private final ITbMessageNoticeService iTbMessageNoticeService;

    /**
     * 查询消息通知列表
     */
    @GetMapping("/list")
    public TableDataInfo<TbMessageNoticeVo> list(TbMessageNoticeBo bo, PageQuery pageQuery) {
        return iTbMessageNoticeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出消息通知列表
     */
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbMessageNoticeBo bo, HttpServletResponse response) {
        List<TbMessageNoticeVo> list = iTbMessageNoticeService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息通知", TbMessageNoticeVo.class, response);
    }

    /**
     * 获取消息通知详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<TbMessageNoticeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTbMessageNoticeService.queryById(id));
    }

    /**
     * 新增消息通知
     */
    @SaCheckPermission("xha-lmt:messageNotice:add")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TbMessageNoticeBo bo) {
        return toAjax(iTbMessageNoticeService.insertByBo(bo));
    }

    /**
     * 修改消息通知
     */
    @SaCheckPermission("xha-lmt:messageNotice:edit")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbMessageNoticeBo bo) {
        return toAjax(iTbMessageNoticeService.updateByBo(bo));
    }

    /**
     * 删除消息通知
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xha-lmt:messageNotice:remove")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTbMessageNoticeService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    //查询消息通知
    @GetMapping("/query")
    public TableDataInfo<TbMessageNoticeVo> selectMessage(String messageType,String workGroup,String status,String startTime,String endTime,PageQuery pageQuery) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = null;
        Date end = null;
        if (startTime != null || endTime != null) {
            start = formatter.parse(startTime);
            end = formatter.parse(endTime);
        }
        return iTbMessageNoticeService.selectMessage(messageType,workGroup,status,start,end,pageQuery);
    }


    //修改成已读状态
    @SaCheckPermission("xha-lmt:messageNotice:updateStatus")
    @Log(title = "更新读取状态", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public R<?> updateStatus(@Validated(EditGroup.class) @RequestBody List<TbMessageNoticeBo> bos) {
        for (TbMessageNoticeBo bo : bos) {
            bo.setStatus("1");
            if (bo.getId() != null) {
                iTbMessageNoticeService.updateByBo(bo);
            }
        }
        return R.ok();
    }

    //查询未读消息数
    @GetMapping("/selectMessageCount")
    public MessageCountVo selectMessageCount() {
        return iTbMessageNoticeService.selectAcceptCount();
    }
}
